﻿using UnityEditor;
using UnityEngine;
using System.Collections;

public class CreateEmptyPrefab : MonoBehaviour
{
    [MenuItem("COCPrefab/Create Prefab From Selected")]
    public static void CreatePrefab()
    {
        GameObject[] objs=Selection.gameObjects;
        foreach(GameObject go  in objs) {
			var localPath = "Assets/Prefabs/" + go.name + ".prefab";
			if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject))) {
				if (EditorUtility.DisplayDialog("Are you sure?", 
					"The prefab already exists. Do you want to overwrite it?", 
					"Yes", 
					"No"))
						CreateNew(go, localPath);
			}
			else
				CreateNew(go, localPath);
		}
    }

    private static void CreateNew(GameObject obj, string localPath)
    {
       	Object prefab= PrefabUtility.CreateEmptyPrefab(localPath);
		PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
        AssetDatabase.Refresh();
    }

    [MenuItem("Examples/Create Prefab From Selected", true)]
	public static bool ValidateCreatePrefab() {
		return Selection.activeGameObject != null;
	}
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}
